package bankaccount;

public class BankAccount {
    //账户余额
    private int balance;

    public BankAccount(int initalbalance) {
        this.balance = initalbalance;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    public synchronized void withdraw(int amount) {
        if (balance >= amount) {
            System.out.println("线程" + Thread.currentThread().getName() + "正在取款 " + amount + "元");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            balance -= amount;
            System.out.println("线程" + Thread.currentThread().getName() + "取款完成，剩余余额：" + balance + "元");
        } else {
            System.out.println("线程" + Thread.currentThread().getName() + "取款失败，余额不足以取款" + balance + "元。当前余额：" + balance + "元");
        }
    }
}
